package cn.edu.scau.cmi.mingjunhui.spring.fx;

import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;

import org.springframework.context.ApplicationContext;

import cn.edu.scau.cmi.mingjunhui.hibernate.DAO.StudentDAO;
import cn.edu.scau.cmi.mingjunhui.hibernate.DAO.TeacherDAO;
import cn.edu.scau.cmi.mingjunhui.hibernate.domain.Student;
import cn.edu.scau.cmi.mingjunhui.hibernate.domain.Teacher;
import cn.edu.scau.cmi.mingjunhui.spring.util.ApplicationContextUtil;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextInputDialog;


public class Index implements Initializable {
	private StudentDAO std;
	private TeacherDAO tea;

	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		ApplicationContext context = ApplicationContextUtil.getContext();
//		setVis(false);
		std = (StudentDAO) context.getBean("studentDAO");
		tea = (TeacherDAO) context.getBean("teacherDAO");
		log.setEditable(false);
	}
	@FXML
	Button addStudent, addTeacher, modStudent, ModTeacher, delStudent, SelTeacher, delTeacher, ModStudent2Teacher, SelStudent, modTeacher2Student;
	@FXML
	TextArea log;


	
	private String getAttr(String name, String title) throws Exception {
		TextInputDialog dialog = new TextInputDialog();
		dialog.setTitle(title);
		dialog.setHeaderText(null);
		dialog.setContentText(name);
		Optional<String> result = dialog.showAndWait();
		// 用户点击取消
		if (!result.isPresent()) {
			throw new Exception("null");
		}
		return result.get();
	}
	
	@FXML
	public void onAddStudent(ActionEvent av) throws Exception {
		Student student = new Student();
		try {
			student.setName(getAttr("学生名字", "添加学生"));
			student.setId(Long.parseLong(getAttr("学生id", "添加学生")));
			std.save(student);
			log.appendText("添加学生：" + student.getName() + "成功\n");
		} catch (Exception e) {
			e.printStackTrace();
			log.appendText("添加学生失败\n");
		}
	}
	@FXML
	public void onAddTea(ActionEvent av) throws Exception {
		Teacher student = new Teacher();
		try {
			student.setName(getAttr("老师名字", "添加老师"));
			student.setId(Long.parseLong(getAttr("老师id", "添加老师")));
			tea.save(student);
			log.appendText("添加老师：" + student.getName() + "成功\n");
		} catch (Exception e) {
//			e.printStackTrace();
			log.appendText("添加老师失败\n");
		}
	}
	@FXML
	public void onModStd(ActionEvent av) throws Exception {
		try {
			Long id = Long.parseLong(getAttr("学生id", "修改学生"));
			Student stu = std.getById(id);
			stu.setName(getAttr("学生名字", "修改学生"));
			std.update(stu);
			log.appendText("修改学生成功\n");
		} catch (Exception e) {
			// TODO: handle exception
			log.appendText("修改学生失败\n");
		}
	}
	@FXML
	public void onModTea(ActionEvent av) throws Exception {
		try {
			Long id = Long.parseLong(getAttr("老师id", "修改老师"));
			Teacher stu = tea.getById(id);
			stu.setName(getAttr("老师名字", "修改老师"));
			tea.update(stu);
			log.appendText("修改老师成功\n");
		} catch (Exception e) {
			// TODO: handle exception
			log.appendText("修改老师失败\n");
		}
	}
	@FXML
	public void onDelStd(ActionEvent av) throws Exception {
		try {
			Long id = Long.parseLong(getAttr("学生id", "删除学生"));
			std.delete(new Student(id));
			log.appendText("删除学生成功\n");
		} catch (Exception e) {
//			e.printStackTrace();
			log.appendText("删除学生失败\n");
		}
	}
	@FXML
	public void onSelTea(ActionEvent av) throws Exception {
		try {
			Long id = Long.parseLong(getAttr("学生id", "学生选择指导老师"));
			Long tId = Long.parseLong(getAttr("老师id", "学生选择指导老师"));
			Student student = std.getById(id);
			student.setTeacher(tId);
			log.appendText("学生选择指导老师成功\n");
		} catch (Exception e) {
//			e.printStackTrace();
			log.appendText("学生选择指导老师失败\n");
		}
	}
	@FXML
	public void onDelTea(ActionEvent av) throws Exception {
		try {
			long id = Long.parseLong(getAttr("老师id", "删除老师"));
			Teacher teacher = tea.getById(id);
			tea.delete(teacher);
			log.appendText("删除老师成功\n");
		} catch (Exception e) {
			// TODO: handle exception
//			e.printStackTrace();
			log.appendText("删除老师失败\n");
		}
	}
	@FXML
	public void onModStd2Tea(ActionEvent av) throws Exception {
		try {
			Long id = Long.parseLong(getAttr("学生id", "学生修改指导教师"));
			Long tId = Long.parseLong(getAttr("老师id", "学生修改指导教师"));
			Student student = std.getById(id);
			student.setTeacher(tId);
			log.appendText("学生修改指导教师成功\n");
		} catch (Exception e) {
//			e.printStackTrace();
			log.appendText("学生修改指导教师失败\n");
		}
	}
	@FXML
	public void onSelStd(ActionEvent av) throws Exception {
		try {
			Long tId = Long.parseLong(getAttr("老师id", "教师选择被指导的学生"));
			Long id = Long.parseLong(getAttr("学生id", "教师选择被指导的学生"));
			Student student = std.getById(id);
			Teacher Nteacher = tea.getById(tId);
			student.setTeacher(tId);
			Nteacher.setStudent(id);;
			log.appendText("教师选择被指导的学生成功\n");
		} catch (Exception e) {
//			e.printStackTrace();
			log.appendText("教师选择被指导的学生失败\n");
		}
	}
	@FXML
	public void onModTea2Std(ActionEvent av) throws Exception {
		try {
			Long tId = Long.parseLong(getAttr("老师id", "教师修改被指导的学生"));
			Long id = Long.parseLong(getAttr("学生id", "教师修改被指导的学生"));
			Student student = std.getById(id);
			Teacher Nteacher = tea.getById(tId);
			student.setTeacher(tId);
			Nteacher.setStudent(id);
			log.appendText("教师修改被指导的学生成功\n");
		} catch (Exception e) {
//			e.printStackTrace();
			log.appendText("教师修改被指导的学生失败\n");
		}
	}
}
